package com.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.entity.Student;
import com.service.StudentService;

@Controller
public class StudentController {
	@Autowired
	private StudentService ss;

	/*
	 * 学生登录
	 */
	@PostMapping(value = "/studentLogin", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject studentLogin(@RequestBody Student student, HttpSession session) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "登录失败";
		Student stu = ss.studentLogin(student.getSno(), student.getPassword());
		if (stu != null) {
			status = 0;
			msg = "登陆成功";
			json.put("status", status);
			json.put("msg", msg);
			// 登录后将用户存在session
			session.setAttribute("student1", stu);
			json.put("stu", stu);
			return json;
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}
	/*
	 * 学生注册
	 */

	@PostMapping(value = "/studentRegister", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject studentRegister(@RequestBody Student student) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "注册失败";
		boolean result = ss.studentRegister(student);
		System.out.println(result);
		if (result) {
			status = 0;
			msg = "注册成功";
			json.put("status", status);
			json.put("msg", msg);
			return json;
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	/*
	 * 退出登录
	 */
	@PostMapping(value = "studentLoginOut")
	@ResponseBody
	public JSONObject studentLoginOut(@RequestBody HttpSession session) {
		JSONObject json = new JSONObject();
		int status = -1;
		String msg = "退出失败";

		try {
			// 销毁session
			session.invalidate();
			status = 0;
			msg = "退出成功";
		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("status", status);
		json.put("message", msg);
		return json;
	}

	/*
	 * 学生修改密码
	 */
	@PostMapping(value = "/updateStudentPassword", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateStudentPassword(String sname,String newpassword,String newpassword2, String oldpassword, HttpSession session) {
		JSONObject json = new JSONObject();
		System.out.println(sname);
		System.out.println(newpassword2);
		System.out.println(oldpassword);
		Student findPasswordByStudentName = ss.findPasswordByStudentName(sname);
		int status = -1;
		String msg = "原密码不正确";
		if (oldpassword.equals(findPasswordByStudentName.getPassword())) {
			
			if (newpassword.equals(newpassword2)) {
				ss.updateStudentPassword(sname, newpassword);
				status = 0;
				msg = "修改成功";
				json.put("status", status);
				json.put("msg", msg);
				return json;
			}else{
				status = -1;
				msg = "输入的两次密码不一样";
				json.put("status", status);
				json.put("msg", msg);
				return json;
			}
		}
		json.put("status", status);
		json.put("msg", msg);
		return json;

	}

	/*
	 * 通过学号查询个人信息
	 */

	@PostMapping(value = "/selectStudentBySno", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject selectStudentBySno(HttpSession session, @RequestBody Student student) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "查询成功";
		Student stu = null;
		try {
			stu = ss.selectStudentBySno(student.getSno());
		} catch (Exception e) {
			e.printStackTrace();
			status = 0;
			msg = "操作失败，请稍后再试";
		}
		json.put("stu", stu);
		json.put("status", status);
		json.put("message", msg);
		return json;
	}

	/*
	 * 修改个人信息
	 */
	@PostMapping(value = "/updateStudent", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateStudent(@RequestBody Student student) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "修改成功";
		boolean result = ss.updateStudent(student);
		json.put("result", result);
		json.put("status", status);
		json.put("message", msg);
		return json;

	}
	
	

}
